<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 多语言模型
 *
 * 功能包括：
 * 	写一列
 * 	写一行
 * 	//写一条资源
 *  取一列
 *  取一行
 *  取一条资源
 *  按条件取 -> 已移到基类
 *  更新一列
 *  更新一行
 *  
 * @author 	FangMinglong
 * @date 	2017-04-12 13:55
 * 
 * @version 1.1.0
 * @date	2017年5月3日 上午10:25:20
 * @desc 	继承基类模型
 */
class Mod_language extends MY_Model
{

	const TABLE_NAME = 'language';
	protected $table_mapper = [
			'master' => 'language',
	];

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	public static function make_pairs($data, $fields)
	{
		$pairs = [];
		foreach ($fields as $field)
		{
			if (!isset($data[$field])) {
				continue;
			}
			$pairs[$field] = $data[$field];
		}
		
		return $pairs;
	}

	/**
	 * 写一列
	 * 
	 * @param  string $module   	模块名称
	 * @param  string $resource 	资源ID
	 * @param  string $language 	语言标识
	 * @param  string $field_name 	字段名称
	 * @param  string $field_value 	字段值
	 * @return int              	ID
	 */
	public function insert_field($module, $resource, $language, $field_name, $field_value)
	{
		$data = [
				'module' => $module,
				'resource' => $resource,
				'language' => $language,
				'field_name' => $field_name,
				'field_value' => $field_value
		];
		
		return $this->db->insert($this->table_mapper['master'], $data);
	}

	/**
	 * 写一行
	 * 
	 * @param  [type] $module   [description]
	 * @param  [type] $resource [description]
	 * @param  [type] $language [description]
	 * @param  array  $pairs    键值对，一维数组
	 * @return array            IDs
	 */
	public function insert_record($module, $resource, $language, $pairs)
	{
		$data = [];
		foreach ($pairs as $field_name => $field_value) {
			$data[] = [
					'module' => $module,
					'resource' => $resource,
					'language' => $language,
					'field_name' => $field_name,
					'field_value' => $field_value
			];
		}
		
		//return $this->db->insert_batch($this->table_mapper['master'], $data);
		foreach ($data as $d) {
			$this->db->replace($this->table_mapper['master'], $d);
		}
		
		return true;
	}
	 
	/**
	 * 写一条资源
	 * 
	 * @param  [type] $module         [description]
	 * @param  [type] $resource       [description]
	 * @param  array  $language_paris 语言键值对，二维数组，键为[language][name]
	 * @return [type]                 [description]
	 */
	public function insert_resource($module, $resource, $language_paris)
	{
		$data = [];
		foreach ($language_paris as $language => $pairs) {
			foreach ($pairs as $field_name => $field_value) {
				$data[] = [
						'module' => $module,
						'resource' => $resource,
						'language' => $language,
						'field_name' => $field_name,
						'field_value' => $field_value
				];
			}
		}
		
		return $this->db->insert_batch($this->table_mapper['master'], $data);
	}

	/**
	 * 取一列
	 * 
	 * @param  [type] $module     [description]
	 * @param  [type] $resource   [description]
	 * @param  [type] $language   [description]
	 * @param  [type] $field_name [description]
	 * 
	 * @return array              返回指定语言下 某一指定字段的值(field_value)信息，一维数组
	 */
	public function select_field($module, $resource, $language, $field_name)
	{
		$where = [
				'module' => $module,
				'resource' => $resource,
				'language' => $language,
				'field_name' => $field_name
		];
		
		$query = $this->db->get_where($this->table_mapper['master'], $where);
		return $query->row_array();
	}

	/**
	 * 取一行
	 * 
	 * @param  [type] $module   [description]
	 * @param  [type] $resource [description]
	 * @param  [type] $language [description]
	 * 
	 * @return array            返回指定语言下 所有字段的键值对(field_name,field_value)信息，二维数组
	 */
	public function select_record($module, $resource, $language)
	{
		$where = [
				'module' => $module,
				'resource' => $resource,
				'language' => $language
		];
		
		$query = $this->db->get_where($this->table_mapper['master'], $where);
		return $query->result_array();
	}

	/**
	 * 取一条资源
	 * 
	 * @param  [type] $module   [description]
	 * @param  [type] $resource [description]
	 * @return [type]           [description]
	 */
	public function select_resource($module, $resource)
	{
		$where = [
				'module' => $module,
				'resource' => $resource
		];
		
		$query = $this->db->get_where($this->table_mapper['master'], $where);
		return $query->result_array();
	}
	
	/**
	 * 更新一列
	 * 
	 * @desc   替换一列：有则修改，无则创建。需要提供所有字段信息。
	 *
	 * @param  [type] $condition [description]
	 * @return [type]            [description]
	 */
	public function update_field($id, $module, $resource, $language, $field_name, $field_value)
	{
		$data = [
				'id' => $id,
				'module' => $module,
				'resource' => $resource,
				'language' => $language,
				'field_name' => $field_name,
				'field_value' => $field_value
		];
		
		return $this->db->replace($this->table_mapper['master'], $data);
	}
	
	/**
	 * 更新一行
	 *
	 * @desc   替换一行：有则修改，无则创建。需要提供 除 id 的 所有字段信息。
	 *
	 * @param  [type] $condition [description]
	 * @return [type]            [description]
	 */
	public function update_record($module, $resource, $language, $pairs)
	{
		$data = [];
		foreach ($pairs as $field_name => $field_value) {
			$data[] = [
					'module' => $module,
					'resource' => $resource,
					'language' => $language,
					'field_name' => $field_name,
					'field_value' => $field_value
			];
		}
		
		foreach ($data as $d) {
			$this->db->replace($this->table_mapper['master'], $d);
		}
		
		return true;
	}

	// public function update_resource(){}
	// public function update_by_condition(){}
}